#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Create by zhang
# Create on 2022/7/26 17:57
import re

from infrastructure.constant.str_content_constant import StrContentConstant
from infrastructure.rules.validators.validator_interface import ValidatorModeEnum, ValidatorInterface
from infrastructure.utils.result_wrap import ResponseWrapper


class IntEntityValidator(ValidatorInterface):
    def __init__(self):
        super(IntEntityValidator, self).__init__()
        self.mode = ValidatorModeEnum.ENTITY
        self.pattern = f"(?![0-9])"

    def validator__(self, value) -> ResponseWrapper:
        if not isinstance(value, int):
            if not isinstance(value, str):
                return ResponseWrapper.fail(msg="必须为数字类型的值")
            else:
                result = re.search(self.pattern, value)
                if result is not None:
                    return ResponseWrapper.fail(msg="必须为数字类型的值")
        return ResponseWrapper.success()


class IntAtomValidator(ValidatorInterface):
    def __init__(self):
        super(IntAtomValidator, self).__init__()
        self.mode = ValidatorModeEnum.ATOM
        self.content = StrContentConstant.NUMBER

    def validator__(self, value) -> ResponseWrapper:
        if not isinstance(value, int):
            if not isinstance(value, str):
                return ResponseWrapper.fail(msg="必须为数字类型的值")
            else:
                for c in value:
                    if c not in self.content:
                        return ResponseWrapper.fail(msg="必须为数字类型的值")
        return ResponseWrapper.success()